﻿using System;
using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.Preferences;

namespace Plumber.Accounting.ModuleData
{
    internal class RegisterModule : ApplicationLibrary.SqlConnector.RegisterModule
    {
        public override string Application()
        {
            return "PLMB";
        }

        public override string Module()
        {
            return "ACCO";
        }

        public override int CurrentVersion()
        {
            return 7;
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        public override void RegisterCountersAndCodes()
        {
            GlobalInfo.AddCounter(Counters.JournalEntry.Int(), Properties.Resources.CN_JournalEntry);
            GlobalInfo.AddCounter(Counters.SaleTaxRegister.Int(), Properties.Resources.CN_SaleTaxRegister);
            GlobalInfo.AddCounter(Counters.PurchaseTaxRegister.Int(), Properties.Resources.CN_PurchaseTaxRegister);
        }

        public override ApplicationLibrary.Preferences.PreferencePanel[] RegisterPreferences()
        {
            return new ApplicationLibrary.Preferences.PreferencePanel[]
            {
                new AccountingPreferencePanel("")
            };
        }

        public override void AddScrounger(ApplicationLibrary.DBManager.iScrounger frm, NameSpace nameSpace)
        {
            if (nameSpace.ApplicationPath == "Plumber.Sales.Forms.SalesInvoice")
                frm.ScroungerList.Add(new Plumber.Accounting.Scrounger.SaleScrounger(frm));

            if (nameSpace.ApplicationPath == "Plumber.Sales.Forms.CreditNote")
                frm.ScroungerList.Add(new Plumber.Accounting.Scrounger.SaleScrounger(frm));

            if (nameSpace.ApplicationPath == "Plumber.Sales.Forms.DebitNote")
                frm.ScroungerList.Add(new Plumber.Accounting.Scrounger.SaleScrounger(frm));

            if (nameSpace.ApplicationPath == "Plumber.Masters.Forms.Company")
                frm.ScroungerList.Add(new Plumber.Accounting.Scrounger.CompanyScrounger(frm));
        }

        protected override bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);

            #region PL_JournalEntry

            if (SearchTable(PL_JournalEntry.Name))
            {
                newTable.NewTable(PL_JournalEntry.Name);

                newTable.AddColumn(PL_JournalEntry.JournalNo);
                newTable.AddColumn(PL_JournalEntry.PostingDate);
                newTable.AddColumn(PL_JournalEntry.Description);
                newTable.AddColumn(PL_JournalEntry.Editable);
                newTable.AddColumn(PL_JournalEntry.Type);
                newTable.AddColumn(PL_JournalEntry.Code);
                newTable.AddColumn(PL_JournalEntry.DocumentNo);
                newTable.AddColumn(PL_JournalEntry.DocumentDate);
                newTable.AddColumn(PL_JournalEntry.TaxRegister);
                newTable.AddColumn(PL_JournalEntry.InvoiceType);

                newTable.AddPrimaryKey(PL_JournalEntry.JournalNo);

                newTable.Create();
            }

            #endregion

            #region PL_JournalEntryDetail

            if (SearchTable(PL_JournalEntryDetail.Name))
            {
                newTable.NewTable(PL_JournalEntryDetail.Name);

                newTable.AddColumn(PL_JournalEntryDetail.JournalNo);
                newTable.AddColumn(PL_JournalEntryDetail.Line);
                newTable.AddColumn(PL_JournalEntryDetail.Type);
                newTable.AddColumn(PL_JournalEntryDetail.Code);
                newTable.AddColumn(PL_JournalEntryDetail.DocumentNo);
                newTable.AddColumn(PL_JournalEntryDetail.Description);
                newTable.AddColumn(PL_JournalEntryDetail.From);
                newTable.AddColumn(PL_JournalEntryDetail.To);
                newTable.AddColumn(PL_JournalEntryDetail.Close);
                newTable.AddColumn(PL_JournalEntryDetail.FromInvoice);

                newTable.AddPrimaryKey(PL_JournalEntry.JournalNo, PL_JournalEntryDetail.Line);

                newTable.Create();
            }

            #endregion

            #region PL_JournalEntryTaxDetail

            if (SearchTable(PL_JournalEntryTaxDetail.Name))
            {
                newTable.NewTable(PL_JournalEntryTaxDetail.Name);

                newTable.AddColumn(PL_JournalEntryTaxDetail.JournalNo);
                newTable.AddColumn(PL_JournalEntryTaxDetail.Line);
                newTable.AddColumn(PL_JournalEntryTaxDetail.TaxCode);
                newTable.AddColumn(PL_JournalEntryTaxDetail.Amount);
                newTable.AddColumn(PL_JournalEntryTaxDetail.Tax);
                newTable.AddColumn(PL_JournalEntryTaxDetail.Deduction);
                newTable.AddColumn(PL_JournalEntryTaxDetail.Restraint);
                newTable.AddColumn(PL_JournalEntryTaxDetail.RestraintCode);
                newTable.AddColumn(PL_JournalEntryTaxDetail.RestraintPerc);
                newTable.AddColumn(PL_JournalEntryTaxDetail.RestraintAmount);
                newTable.AddColumn(PL_JournalEntryTaxDetail.BankRestraint);

                newTable.AddPrimaryKey(PL_JournalEntryTaxDetail.JournalNo, PL_JournalEntryTaxDetail.Line);

                newTable.Create();
            }

            #endregion

            #region PL_JETemplate

            if (SearchTable(PL_JETemplate.Name))
            {
                newTable.NewTable(PL_JETemplate.Name);

                newTable.AddColumn(PL_JETemplate.Template);
                newTable.AddColumn(PL_JETemplate.PostingDate);
                newTable.AddColumn(PL_JETemplate.Type);
                newTable.AddColumn(PL_JETemplate.Code);
                newTable.AddColumn(PL_JETemplate.Description);
                newTable.AddColumn(PL_JETemplate.TaxRegister);

                newTable.AddPrimaryKey(PL_JETemplate.Template);

                newTable.Create();
            }

            #endregion

            #region PL_JETemplateDetail

            if (SearchTable(PL_JETemplateDetail.Name))
            {
                newTable.NewTable(PL_JETemplateDetail.Name);

                newTable.AddColumn(PL_JETemplateDetail.Template);
                newTable.AddColumn(PL_JETemplateDetail.Line);
                newTable.AddColumn(PL_JETemplateDetail.Type);
                newTable.AddColumn(PL_JETemplateDetail.Code);
                newTable.AddColumn(PL_JETemplateDetail.DocumentNo);
                newTable.AddColumn(PL_JETemplateDetail.Description);
                newTable.AddColumn(PL_JournalEntryDetail.From);
                newTable.AddColumn(PL_JETemplateDetail.To);

                newTable.AddPrimaryKey(PL_JETemplateDetail.Template, PL_JETemplateDetail.Line);

                newTable.Create();
            }

            #endregion

            #region PL_JETemplateTaxDetail

            if (SearchTable(PL_JETemplateTaxDetail.Name))
            {
                newTable.NewTable(PL_JETemplateTaxDetail.Name);

                newTable.AddColumn(PL_JETemplateTaxDetail.Template);
                newTable.AddColumn(PL_JETemplateTaxDetail.Line);
                newTable.AddColumn(PL_JETemplateTaxDetail.TaxCode);
                newTable.AddColumn(PL_JETemplateTaxDetail.Amount);
                newTable.AddColumn(PL_JETemplateTaxDetail.Tax);
                newTable.AddColumn(PL_JournalEntryTaxDetail.Deduction);

                newTable.AddPrimaryKey(PL_JETemplateTaxDetail.Template, PL_JETemplateDetail.Line);

                newTable.Create();
            }

            #endregion

            #region PL_LiquidationTax

            if (SearchTable(PL_LiquidationTax.Name))
            {
                newTable.NewTable(PL_LiquidationTax.Name);

                newTable.AddColumn(PL_LiquidationTax.Year);
                newTable.AddColumn(PL_LiquidationTax.Month);
                newTable.AddColumn(PL_LiquidationTax.SaleDebit);
                newTable.AddColumn(PL_LiquidationTax.SaleCredit);
                newTable.AddColumn(PL_LiquidationTax.PurchaseDebit);
                newTable.AddColumn(PL_LiquidationTax.PurchaseCredit);
                newTable.AddColumn(PL_LiquidationTax.PrevoiusCredit);
                newTable.AddColumn(PL_LiquidationTax.CurrentCredit);
                newTable.AddColumn(PL_LiquidationTax.JournalEntryNo);
                newTable.AddColumn(PL_LiquidationTax.SaleRegister);
                newTable.AddColumn(PL_LiquidationTax.PurchaseRegister);

                newTable.AddPrimaryKey(PL_LiquidationTax.Year, PL_LiquidationTax.Month);

                newTable.Create();
            }

            #endregion

            #region PL_LiquidationType

            if (SearchTable(PL_LiquidationType.Name))
            {
                newTable.NewTable(PL_LiquidationType.Name);

                newTable.AddColumn(PL_LiquidationType.Company);
                newTable.AddColumn(PL_LiquidationType.Year);
                newTable.AddColumn(PL_LiquidationType.Type);

                newTable.AddPrimaryKey(PL_LiquidationType.Company, PL_LiquidationType.Year);

                newTable.Create();
            }

            #endregion
            return true;
        }

        protected override bool UpdateDBTables()
        {
            if (dbVersion < 2)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_JournalEntry.DocumentNo))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.DocumentNo);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<string>(PL_JournalEntry.DocumentNo, "");
                    qb.AddCompareIsNull(PL_JournalEntry.DocumentNo);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntry.TaxRegister))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.TaxRegister);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<TaxRegisterType>(PL_JournalEntry.TaxRegister, TaxRegisterType.E_NORegister);
                    qb.AddCompareIsNull(PL_JournalEntry.TaxRegister);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntry.Type))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.Type);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<string>(PL_JournalEntry.Type, "");
                    qb.AddCompareIsNull(PL_JournalEntry.Type);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntry.Code))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.Code);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<string>(PL_JournalEntry.Code, "");
                    qb.AddCompareIsNull(PL_JournalEntry.Code);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntry.DocumentDate))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.DocumentDate);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<DateTime>(PL_JournalEntry.DocumentDate, PL_JournalEntry.EmptyDate);
                    qb.AddCompareIsNull(PL_JournalEntry.DocumentDate);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JETemplate.TaxRegister))
                {
                    alterMaster.AlterTable(PL_JETemplate.Name);
                    alterMaster.AddColumn(PL_JETemplate.TaxRegister);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JETemplate.Name);
                    qb.AddSet<TaxRegisterType>(PL_JETemplate.TaxRegister, TaxRegisterType.E_NORegister);
                    qb.AddCompareIsNull(PL_JETemplate.TaxRegister);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 3)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_JournalEntryTaxDetail.Restraint))
                {
                    alterMaster.AlterTable(PL_JournalEntryTaxDetail.Name);
                    alterMaster.AddColumn(PL_JournalEntryTaxDetail.Restraint);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntryTaxDetail.Name);
                    qb.AddSet<bool>(PL_JournalEntryTaxDetail.Restraint, false);
                    qb.AddCompareIsNull(PL_JournalEntryTaxDetail.Restraint);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntryTaxDetail.RestraintCode))
                {
                    alterMaster.AlterTable(PL_JournalEntryTaxDetail.Name);
                    alterMaster.AddColumn(PL_JournalEntryTaxDetail.RestraintCode);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntryTaxDetail.Name);
                    qb.AddSet<string>(PL_JournalEntryTaxDetail.RestraintCode, "");
                    qb.AddCompareIsNull(PL_JournalEntryTaxDetail.RestraintCode);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntryTaxDetail.RestraintPerc))
                {
                    alterMaster.AlterTable(PL_JournalEntryTaxDetail.Name);
                    alterMaster.AddColumn(PL_JournalEntryTaxDetail.RestraintPerc);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntryTaxDetail.Name);
                    qb.AddSet<double>(PL_JournalEntryTaxDetail.RestraintPerc, 0);
                    qb.AddCompareIsNull(PL_JournalEntryTaxDetail.RestraintPerc);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JournalEntryTaxDetail.RestraintAmount))
                {
                    alterMaster.AlterTable(PL_JournalEntryTaxDetail.Name);
                    alterMaster.AddColumn(PL_JournalEntryTaxDetail.RestraintAmount);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntryTaxDetail.Name);
                    qb.AddSet<double>(PL_JournalEntryTaxDetail.RestraintAmount, 0);
                    qb.AddCompareIsNull(PL_JournalEntryTaxDetail.RestraintAmount);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 4)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_JournalEntry.InvoiceType))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.InvoiceType);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<int>(PL_JournalEntry.InvoiceType, InvoiceType.E_InvoiceEm.Int());
                    qb.AddCompare<int>(PL_JournalEntry.TaxRegister, TaxRegisterType.E_TaxSale.Int());
                    qb.AddCompareIsNull(PL_JournalEntry.InvoiceType);
                    int nRow = qb.ExecuteNonQuery();

                    QueryBuilder qb1 = new QueryBuilder(false, true);
                    qb1.Update(PL_JournalEntry.Name);
                    qb1.AddSet<int>(PL_JournalEntry.InvoiceType, InvoiceType.E_InvoiceRc.Int());
                    qb1.AddCompare<int>(PL_JournalEntry.TaxRegister, TaxRegisterType.E_TaxPurchase.Int());
                    qb1.AddCompareIsNull(PL_JournalEntry.InvoiceType);
                    nRow = qb1.ExecuteNonQuery();

                    QueryBuilder qb2 = new QueryBuilder(false, true);
                    qb2.Update(PL_JournalEntry.Name);
                    qb2.AddSet<int>(PL_JournalEntry.InvoiceType, InvoiceType.E_NoInvoice.Int());
                    qb2.AddCompareIsNull(PL_JournalEntry.InvoiceType);
                    nRow = qb2.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JETemplate.InvoiceType))
                {
                    alterMaster.AlterTable(PL_JETemplate.Name);
                    alterMaster.AddColumn(PL_JETemplate.InvoiceType);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JETemplate.Name);
                    qb.AddSet<int>(PL_JETemplate.InvoiceType, InvoiceType.E_InvoiceEm.Int());
                    qb.AddCompare<int>(PL_JETemplate.TaxRegister, TaxRegisterType.E_TaxSale.Int());
                    qb.AddCompareIsNull(PL_JETemplate.InvoiceType);
                    int nRow = qb.ExecuteNonQuery();

                    QueryBuilder qb1 = new QueryBuilder(false, true);
                    qb1.Update(PL_JETemplate.Name);
                    qb1.AddSet<int>(PL_JETemplate.InvoiceType, InvoiceType.E_InvoiceRc.Int());
                    qb1.AddCompare<int>(PL_JETemplate.TaxRegister, TaxRegisterType.E_TaxPurchase.Int());
                    qb1.AddCompareIsNull(PL_JETemplate.InvoiceType);
                    nRow = qb1.ExecuteNonQuery();

                    QueryBuilder qb2 = new QueryBuilder(false, true);
                    qb2.Update(PL_JETemplate.Name);
                    qb2.AddSet<int>(PL_JETemplate.InvoiceType, InvoiceType.E_NoInvoice.Int());
                    qb2.AddCompareIsNull(PL_JETemplate.InvoiceType);
                    nRow = qb2.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JETemplate.Type))
                {
                    alterMaster.AlterTable(PL_JETemplate.Name);
                    alterMaster.AddColumn(PL_JETemplate.Type);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JETemplate.Name);
                    qb.AddSet<string>(PL_JETemplate.Type, "");
                    qb.AddCompareIsNull(PL_JETemplate.Type);
                    int nRow = qb.ExecuteNonQuery();
                }

                if (!SearchColumn(PL_JETemplate.Code))
                {
                    alterMaster.AlterTable(PL_JETemplate.Name);
                    alterMaster.AddColumn(PL_JETemplate.Code);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JETemplate.Name);
                    qb.AddSet<string>(PL_JETemplate.Code, "");
                    qb.AddCompareIsNull(PL_JETemplate.Code);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 5)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_JournalEntryTaxDetail.BankRestraint))
                {
                    alterMaster.AlterTable(PL_JournalEntryTaxDetail.Name);
                    alterMaster.AddColumn(PL_JournalEntryTaxDetail.BankRestraint);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntryTaxDetail.Name);
                    qb.AddSet<bool>(PL_JournalEntryTaxDetail.BankRestraint, false);
                    qb.AddCompareIsNull(PL_JournalEntryTaxDetail.BankRestraint);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 6)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_JournalEntry.InvoiceNo))
                {
                    alterMaster.AlterTable(PL_JournalEntry.Name);
                    alterMaster.AddColumn(PL_JournalEntry.InvoiceNo);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntry.Name);
                    qb.AddSet<string>(PL_JournalEntry.InvoiceNo, "");
                    qb.AddCompareIsNull(PL_JournalEntry.InvoiceNo);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            if (dbVersion < 7)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(PL_JournalEntryDetail.InvoiceNo))
                {
                    alterMaster.AlterTable(PL_JournalEntryDetail.Name);
                    alterMaster.AddColumn(PL_JournalEntryDetail.InvoiceNo);
                    alterMaster.Alter();

                    QueryBuilder qb = new QueryBuilder(false, true);
                    qb.Update(PL_JournalEntryDetail.Name);
                    qb.AddSet<string>(PL_JournalEntryDetail.InvoiceNo, "");
                    qb.AddCompareIsNull(PL_JournalEntryDetail.InvoiceNo);
                    int nRow = qb.ExecuteNonQuery();
                }
            }

            return true;
        }
    }
}